﻿using System;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Sockets;

namespace FileSC.WEBService.Client
{
    internal class NasApi
    {
        internal HttpClient client { get; set; }
        internal HttpRequest request { get; set; }
        internal TouchSocketConfig config { get; set; }

        internal NasApi(string ip_uri)
        {
            client = new HttpClient();
            request = new HttpRequest(client);
            config = new TouchSocketConfig();
            config.SetRemoteIPHost(ip_uri);
        }

        internal async Task<HttpResponseResult> Post(string uri)
        {
            await Connect();
            var _uri = new Uri(uri);
            request.InitHeaders()
                .SetUrl(_uri.PathAndQuery)
                .SetHost(_uri.Host)
                .AsPost();
            var responseResult = await client.RequestAsync(request, int.MaxValue);
            return responseResult;
        }
        internal async Task<HttpResponseResult> Get(string uri)
        {
            await Connect();
            var _uri = new Uri(uri);
            request.InitHeaders()
                .SetUrl(_uri.PathAndQuery)
                .SetHost(_uri.Host)
                .AsGet();
            var responseResult = await client.RequestAsync(request, int.MaxValue);
            return responseResult;
        }

        internal void AddHeader(string name, string value)
        {
            request.AddHeader(name, value);
        }
        internal void AddContent(HttpContent content)
        {
            request.SetContent(content);
        }
        internal async Task Connect()
        {
            //配置config
            await client.SetupAsync(config);
            await client.ConnectAsync();//先做连接
        }
    }
}
